package com.yjt.gcss.gddw.common.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.jdbc.Work;
import org.springframework.stereotype.Service;

import com.yjt.common.easyui.datagrid.DataGridDAOImpl;
import com.yjt.common.hibernate.base.DaoException;
import com.yjt.gcss.gddw.common.bean.domain.UiFileLog;

@Service(UiFileLogDao.BEAN_PACKAGE_NAME)
public class UiFileLogDao extends DataGridDAOImpl {

	public static final String BEAN_PACKAGE_NAME = "com.yjt.gcss.gddw.common.dao.UiFileLogDao";
	public UiFileLogDao(){

	}

	public UiFileLog getBeanById(String id) throws DaoException {
		return (UiFileLog) getBeanById(UiFileLog.BEAN_PACKAGE_NAME, id, false);
	}
	
	public UiFileLog getBeanByIdForUpdate(String id) throws DaoException {
		return (UiFileLog) getBeanById(UiFileLog.BEAN_PACKAGE_NAME, id, true);
	}

	@SuppressWarnings("rawtypes")
	public Collection getAll() throws DaoException {
		return getAll(UiFileLog.BEAN_PACKAGE_NAME);
	}

	@SuppressWarnings("rawtypes")
	public UiFileLog getBeanByParams(String hqlName,ArrayList paraList) throws DaoException{
		return (UiFileLog) getProtoBeanByParams(hqlName,paraList);
	}

	public UiFileLog getBeanByBean(UiFileLog bean, MatchMode mode) throws DaoException{
		return (UiFileLog) getProtoBeanByBean(bean, mode);
	}
	
	public UiFileLogDao setOrder(Order order){
		return (UiFileLogDao)super.addOrder(order);
	}
	
	/**
	 * ����mysql�洢����
	 * @param fileId
	 * @return
	 */
	public boolean updateUiFileForResponse(final String fileId) {
		Session session = getSessionFactory().getCurrentSession();
        final Map<String,Integer> result = new HashMap<String, Integer>();   
		session.doWork(new Work() {
            @Override  
            public void execute(Connection conn) throws SQLException   
            {   
                CallableStatement proc = null;   
                try  
                {   
                    proc = conn.prepareCall("{CALL SPROC_GDDW_UIFILELOG_UPDATE_RSP(?,?)}");   
                    //ע�⣺�����Ǵ����������   
                    proc.setString(1, fileId);   
                    //ע�⣺������ע���������   
                    proc.registerOutParameter(2, java.sql.Types.VARCHAR);   
                    //ִ�д洢����   
                    proc.execute();   
                    //��ȡִ����Ĵ洢���̵ķ���ֵ ������ֵ�Ĳ���λ��
                    result.put("finished", Integer.parseInt(proc.getString(2)));   
                }   
                catch(Exception e)
                {   
                    throw new DaoException(e);
                }   
                finally  
                {   
                    if(null != proc)   
                    {   
                        proc.close();
                        proc = null;
                    }   
                }   
            }   
        });
		//SELECT COUNT(*) INTO OUTPUT_FINISHED_COUNT FROM UI_FILE_LOG WHERE `STATUS` = 'S' AND FILE_STATUS = 'S';
		if(result.get("finished") == 1) {
			return true;
		}
        return false;   
	}
	
	
	/**
	 * mysql : truncate table UI_FILE_LOG
	 * @throws DaoException
	 */
	public int testTruncateUiFileLogForMySql() throws DaoException {
		Transaction tx = null;
		Session session = null;
		int countInt = 0;
		try {
			session = getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			//ֻд��ֻ֧��mysql
			countInt = session.createSQLQuery("truncate table UI_FILE_LOG").executeUpdate();
			tx.commit();
		} catch(Exception e) {
			throw new DaoException(e);
		} finally {
			if(session != null) {
				session.close();
			}
		}
		return countInt;
	}
}
